//
//  XHShopCartManager.m
//  xinhua
//
//  Created by fengyn on 15/11/24.
//  Copyright © 2015年 lexingsoft. All rights reserved.
//

static NSString *KeyShopCartProductArray = @"KeyShopCartProductArray";
static NSString *KeyShopCartProductCountDic = @"KeyShopCartProductCountDic";
#import "XHShopCartManager.h"

@interface XHShopCartManager()
@property (nonatomic, strong) NSMutableArray *selectedProductArray;
@end
@implementation XHShopCartManager
// 获取使用实例
+ (instancetype )sharedInstance
{
    static XHShopCartManager *sharedInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedInstance = [[super alloc] init];
        [sharedInstance initManager];
    });
    return sharedInstance;
}

//初始化Manager
- (void) initManager
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:KeyShopCartProductArray]) {
        _productArray=[NSMutableArray arrayWithArray:[NSString jsonObjectWithNSString:[userDefaults objectForKey:KeyShopCartProductArray]]];
    }else{
        _productArray=[NSMutableArray array];
    }
    if ([userDefaults objectForKey:KeyShopCartProductCountDic]) {
        _productCountDic=[NSMutableDictionary dictionaryWithDictionary:[NSString jsonObjectWithNSString:[userDefaults objectForKey:KeyShopCartProductCountDic]]];
    }else{
        _productCountDic=[NSMutableDictionary dictionary];
    }
    self.selectedProductArray=[NSMutableArray array];
}

//保存用户数据
- (void) saveData
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:[NSString jsonStringWithNSDictionary:self.productArray] forKey:KeyShopCartProductArray];
        [userDefaults setObject:[NSString jsonStringWithNSDictionary:self.productCountDic] forKey:KeyShopCartProductCountDic];
        [userDefaults synchronize];
    });
}

/// 增加一个商品至购物车(数量+1)
- (void)addOneProduct:(NSDictionary *)productInfoDic
{
    NSString *foodKey=[NSString stringWithFormat:@"%@",productInfoDic[@"sequenceNBR"]];
    NSNumber *number=[self.productCountDic objectForKey:foodKey];
    if (number) {
        [self.productCountDic setObject:@([number integerValue]+1) forKey:foodKey];
    }else
    {
        [self.productCountDic setObject:@1 forKey:foodKey];
        [self.productArray addObject:productInfoDic];
    }
    [self saveData];
}

/// 从购物车去掉一个商品(数量-1)
- (void)removeOneProduct:(NSDictionary *)productInfoDic
{
    NSString *foodKey=[NSString stringWithFormat:@"%@",productInfoDic[@"sequenceNBR"]];
    NSNumber *number=[self.productCountDic objectForKey:foodKey];
    NSInteger count=[number integerValue]-1;
    if (count<=0) {
        [self.productCountDic removeObjectForKey:foodKey];
        [self.productArray removeObject:productInfoDic];
        [self setProduct:productInfoDic isSelected:NO];
    }else
    {
        [self.productCountDic setObject:@(count) forKey:foodKey];
    }
    [self saveData];
}

/// 从购物车中彻底删除一个商品
- (void)removeTheProduct:(NSDictionary *)productInfoDic
{
    NSString *foodKey=[NSString stringWithFormat:@"%@",productInfoDic[@"sequenceNBR"]];
    [self.productCountDic removeObjectForKey:foodKey];
    [self.productArray removeObject:productInfoDic];
    [self setProduct:productInfoDic isSelected:NO];
    [self saveData];
}

/// 清空购物车
- (void)clearAllProduct
{
    _productCountDic=[NSMutableDictionary dictionary];
    _productArray=[NSMutableArray array];
    _selectedProductArray=[NSMutableArray array];
    [self saveData];
}

/// 获取购物车中对应商品数量
- (NSInteger)numberOfProduct:(NSDictionary *)productInfoDic
{
    NSString *foodKey=[NSString stringWithFormat:@"%@",productInfoDic[@"sequenceNBR"]];
    NSNumber *number=[self.productCountDic objectForKey:foodKey];
    return [number integerValue];
}

/// 获取购物车中商品总数
- (NSInteger)numberOfAllProduct
{
    NSInteger allCount=0;
    for (NSDictionary *dic in self.productArray) {
        NSString *foodKey=[NSString stringWithFormat:@"%@",dic[@"sequenceNBR"]];
        NSNumber *number=[self.productCountDic objectForKey:foodKey];
        if (!number) {
            number=[NSNumber numberWithInt:1];
        }
        allCount += [number integerValue];
    }
    return allCount;
}

/// 获取购物车商品总价
- (double)priceOfAllProduct
{
    double allPrice=0;
    for (NSDictionary *dic in self.productArray) {
        NSString *foodKey=[NSString stringWithFormat:@"%@",dic[@"sequenceNBR"]];
        NSNumber *number=[self.productCountDic objectForKey:foodKey];
        if (!number) {
            number=[NSNumber numberWithInt:1];
        }
        double price=[dic[@"bookPrice"] doubleValue];
        allPrice += ([number integerValue] *price);
    }
    return allPrice;
}

/// 获取购物车中选中商品总价
- (double)priceOfAllSelectedProduct
{
    double allPrice=0;
    for (NSDictionary *dic in self.productArray) {
        if ([self isSelectedProduct:dic]) {
            NSString *foodKey=[NSString stringWithFormat:@"%@",dic[@"sequenceNBR"]];
            NSNumber *number=[self.productCountDic objectForKey:foodKey];
            if (!number) {
                number=[NSNumber numberWithInt:1];
            }
            double price=[dic[@"bookPrice"] doubleValue];
            allPrice += ([number integerValue] *price);
        }
    }
    return allPrice;
}

/// 清除购物车中已经结算的商品
- (void)removeSelectedProduct{
    NSArray *selectedArrays=[self selectedProductInfoArray];
    for (NSDictionary *dic in selectedArrays) {
        [self removeTheProduct:dic];
    }
    self.selectedProductArray=[NSMutableArray array];
}

/// 获取选中商品数组
- (NSArray *)selectedProductInfoArray{
    NSMutableArray *selectedArray=[NSMutableArray array];
    for ( NSDictionary *dic in self.productArray) {
        if ([self isSelectedProduct:dic]) {
            [selectedArray addObject:dic];
        }
    }
    return selectedArray;
}

/// 获取购物车中选中商品数量
- (NSDictionary *)selectedProductCountDic{
    NSMutableDictionary *selectedDic=nil;
    for ( NSString *foodKey in self.selectedProductArray) {
        if (!selectedDic) {
            selectedDic=[NSMutableDictionary dictionary];
        }
        [selectedDic setObject:self.productCountDic[foodKey] forKey:foodKey];
    }
    return selectedDic;
}

/// 是否选中
- (BOOL)isSelectedProduct:(NSDictionary *)productInfoDic
{
    NSString *foodKey=[NSString stringWithFormat:@"%@",productInfoDic[@"sequenceNBR"]];
    for (NSString *value in self.selectedProductArray) {
        if ([value isEqualToString:foodKey]) {
            return YES;
        }
    }
    return NO;
}

/// 设置是否选中些商品
- (void)setProduct:(NSDictionary *)productInfoDic isSelected:(BOOL)isSelected
{
    NSString *foodKey=[NSString stringWithFormat:@"%@",productInfoDic[@"sequenceNBR"]];
    if (isSelected) {
        [self.selectedProductArray addObject:foodKey];
    }else
    {
        [self.selectedProductArray removeObject:foodKey];
    }
}
@end
